<?php
	class Account extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			if (!isset($_SESSION)) {
			    session_start();
			    session_regenerate_id();
			}
			$this->load->helper("url");
			
		}
		public function index()
		{
			if(isset($_SESSION['accountdeal'])){
				$mang=$_SESSION['accountdeal'];
				echo '<script>
						window.location.assign("'.base_url('home').'");
						</script>';
			}
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$data['action']="login";
			$this->load->view("main_template", $data);
		}
		public function login()
		{
			if(isset($_SESSION['accountdeal'])){
				$mang=$_SESSION['accountdeal'];
				echo '<script>
						window.location.assign("'.base_url('home').'");
						</script>';
			}
			
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$this->load->model("mpayment");
			$data['list_city']=$this->mpayment->list_city();
			$data['list_district']=$this->mpayment->list_district();
			
			$data['action']="login";
			$this->load->view("main_template", $data);
		}
		public function register()
		{
			if(isset($_SESSION['accountdeal'])){
				$mang=$_SESSION['accountdeal'];
				echo '<script>
						window.location.assign("'.base_url('home').'");
						</script>';
			}
			
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$this->load->model("mpayment");
			$data['list_city']=$this->mpayment->list_city();
			$data['list_district']=$this->mpayment->list_district();
			
			
			$data['action']="register";
			$this->load->view("main_template", $data);
		}
		public function changecity()
		{
			$id_city=$_REQUEST['id_city'];
			$this->load->model("mpayment");
			$data=$this->mpayment->list_district_city($id_city);
			$body='<select style="width: 180px; height:24px; border:#ccc solid 1px;" name="district" id="district">
              <option value="0" selected="selected">Chọn Quận Huyện</option>';
			foreach ($data as $value =>$giatri){
				$body.='<option value="'.$giatri['id'].'" >'.$giatri['name'].'</option>';
			}
            $body.='</select>';
			echo $body;
		}
		public function register_ok()
		{
			if(isset($_SESSION['accountdeal'])){
				$mang=$_SESSION['accountdeal'];
				echo '<script>
						window.location.assign("'.base_url('home').'");
						</script>';
			}
			
			$email=$_REQUEST['email'];
			$name=$_REQUEST['name'];
			$passwd=$_REQUEST['npassword'];
			$sex=$_REQUEST['sex'];
			$phone=$_REQUEST['phone'];
			$address=$_REQUEST['address'];
			$city=$_REQUEST['city'];
			$district=$_REQUEST['district'];
			$type=$_REQUEST['type'];
			$this->load->model("maccount");
			$data=$this->maccount->register_ok($email,$name,$passwd,$sex,$phone,$address,$city,$district,$type);
			echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
		}
		public function login_ok()
		{
			if(isset($_SESSION['accountdeal'])){
				$mang=$_SESSION['accountdeal'];
				echo '<script>
						window.location.assign("'.base_url('home').'");
						</script>';
			}
				$lemail=$_REQUEST['lemail'];
				$lpassword=$_REQUEST['lpassword'];
				
				
				$this->load->model("maccount");
				$data=$this->maccount->login_ok($lemail,$lpassword);
				if(count($data)>0){
					$mang=array($data[0]['id'],$data[0]['email'],$data[0]['name'],$data[0]['sex'],$data[0]['phone'],$data[0]['address'],$data[0]['city'],$data[0]['district'],$data[0]['type']);
					$_SESSION['accountdeal']=$mang;
					
					if(isset($_SESSION['backlinkweb'])){
						$link1=$_SESSION['backlinkweb'];
						unset($_SESSION['backlinkweb']);
						echo '<script>
						window.location.assign("'.$link1.'");
						</script>';
					}else{
						echo '<script>
							window.location.assign("'.base_url('home').'");
						</script>';
					}
				}else{
					echo '<script>
							alert("Dang nhap khong thanh cong !!! Ban vui long kiem tra lai tai khoan hay mat khau !!!");
							window.location.assign("'.base_url('account/login').'");
						</script>';
				}
		
			
		}
		
		public function logout()
		{
			unset($_SESSION['cart']);
			unset($_SESSION['accountdeal']);
			
						echo '<script>
							window.location.assign("'.base_url('home').'");
						</script>';

				
		
			
		}
		public function person()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$this->load->model("mpayment");
			$data['list_city']=$this->mpayment->list_city();
			$data['list_district']=$this->mpayment->list_district_city($mang[6]);
			
			
			$data['action']="person";
			$this->load->view("main_template", $data);
		}
		public function updateperson_ok()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			
			$name=$_REQUEST['name'];
			$sex=$_REQUEST['sex'];
			$phone=$_REQUEST['phone'];
			$address=$_REQUEST['address'];
			$city=$_REQUEST['city'];
			$district=$_REQUEST['district'];
			
			$id_account=$_REQUEST['id_account'];
			
			$this->load->model("maccount");
			$data=$this->maccount->updateperson_ok($id_account,$name,$sex,$phone,$address,$city,$district);
			$mang=$_SESSION['accountdeal'];
			$mang[2]=$name;
			$mang[3]=$sex;
			$mang[4]=$phone;
			$mang[5]=$address;
			$mang[6]=$city;
			$mang[7]=$district;
			$_SESSION['accountdeal']=$mang;
			echo '<script>
						alert("Update successful !!!");
						window.location.assign("'.base_url('account/person').'");
						</script>';
		}
		
		public function confirm_password()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			$opassword=$_REQUEST['opassword'];
			$this->load->model("maccount");
			$data=$this->maccount->confirm_password($mang[0],$opassword);
			echo count($data);
		}
		
		public function changepassword_ok()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			$npassword=$_REQUEST['cpassword1'];
			$this->load->model("maccount");
			$data=$this->maccount->changepassword_ok($mang[0],$npassword);
			echo '<script>
						alert("Change Password successful !!!");
						window.location.assign("'.base_url('account/person').'");
						</script>';
		}
		
		public function add_deal()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			if($mang[8]==0){
				echo '<script>
						window.location.assign("'.base_url('account/person').'");
						</script>';
			}
			
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$data['action']="add_deal";
			$this->load->view("main_template", $data);
		}
		
		public function add_deal_ok()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			
			$idaccount=0;

			if(isset($_SESSION['accountdeal'])){
							$mang=$_SESSION['accountdeal'];
					$idaccount=$mang[0];		
			}
			$did_category=trim($_REQUEST['did_category']);
			
			$uploadDir1 = 'upload/'.$idaccount.'/deal/'.$did_category.'/';
			$uploadDir = 'upload/'.$idaccount.'/deal/'.$did_category.'/';
			
			if (!file_exists($uploadDir)) mkdir($uploadDir, 0777, true);
			
			$deal=strtotime('now');
			if(isset($_FILES['dsmall_image'])){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dsmall_image']['name']);
						$tmpName = $_FILES['dsmall_image']['tmp_name'];
						$fileSize = $_FILES['dsmall_image']['size'];
						$fileType = $_FILES['dsmall_image']['type'];
						$fileName="small_".$deal.'.jpg';
						$filePath1 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dsmall_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dsmall_image']['type'], $acceptable) && !empty($_FILES["dsmall_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath1)) {
				        //echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
			}
			$filePath1 = $uploadDir1 . $fileName;

			if(isset($_FILES['dlarge_image'])){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dlarge_image']['name']);
						$tmpName = $_FILES['dlarge_image']['tmp_name'];
						$fileSize = $_FILES['dlarge_image']['size'];
						$fileType = $_FILES['dlarge_image']['type'];
						$fileName="large_".$deal.'.jpg';
						$filePath2 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dlarge_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dlarge_image']['type'], $acceptable) && !empty($_FILES["dlarge_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath2)) {
				        //echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
			}
			$filePath2 = $uploadDir1 . $fileName;
			
			
			
			$dname=trim($_REQUEST['dname']);
			$dname = str_replace("'", "\'", ($dname));
			
			$dprice=trim($_REQUEST['dprice']);
			$dprice = str_replace( ',', '', $dprice);
			
			$dsale_off=trim($_REQUEST['dsale_off']);
			$dsale_off=str_replace( ',', '.', $dsale_off);
			
			$dprice_sale_off=trim($_REQUEST['dprice_sale_off']);
			$dprice_sale_off = str_replace( ',', '', $dprice_sale_off);
			
			$dtitle=str_replace("'", "", ($_REQUEST['dtitle']));
			
			$dadvantage=str_replace("'", "", ($_REQUEST['dadvantage']));
			$dcondition=str_replace("'", "", ($_REQUEST['dcondition']));
			$dcontent=str_replace("'", "", ($_REQUEST['dcontent']));
			
			$top_seller=0;
			if(isset($_REQUEST['top_seller']))
			$top_seller=1;
			
			$top_view=0;
			if(isset($_REQUEST['top_view']))
			$top_view=1;
			
			$top_100=0;
			if(isset($_REQUEST['top_100']))
			$top_100=1;
			
			$freeship=0;
			if(isset($_REQUEST['freeship']))
			$freeship=1;
			
			$delivery=trim($_REQUEST['delivery']);
			
			$this->load->model("mdeal");
			$this->mdeal->add_deal($did_category,$dname,$dprice,$dsale_off,$dprice_sale_off,$filePath1,$filePath2,$dtitle,$dadvantage,$dcondition,$dcontent,$top_seller,$top_view,$top_100,$freeship,$delivery,$idaccount);
			echo '<script>
					alert("Add Deal successful");
						window.location.assign("'.base_url('account/list_deal_account').'");
						</script>';
			
		}
		public function list_deal_account()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			if($mang[8]==0){
				echo '<script>
						window.location.assign("'.base_url('account/person').'");
						</script>';
			}
			
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$data['action']="list_deal";
			
			$cid=0;
			if(isset($_REQUEST['cid']))
				$cid=$_REQUEST['cid'];
			$this->load->model("mdeal");
			$data['list_deal_account']=$this->mdeal->list_deal_account($mang[0]);
			$data['action']='list_deal_account';
			
			$this->load->view("main_template", $data);
		}
		
		
		public function edit_deal()
		{
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			$mang=$_SESSION['accountdeal'];
			if($mang[8]==0){
				echo '<script>
						window.location.assign("'.base_url('account/person').'");
						</script>';
			}
			
			$data=array();
			
			$did=$_REQUEST['did'];
			$data['did']=$did;
			
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			$data['deal_detail']=$this->mdeal->get_deal($did);	
			
			$data['action']="edit_deal";
			$this->load->view("main_template", $data);
		}
		public function edit_deal_ok()
		{
			$id_deal=trim($_REQUEST['id_deal']);
			$did_category=trim($_REQUEST['did_category']);
			
			if(!isset($_SESSION['accountdeal'])){
				echo '<script>
						window.location.assign("'.base_url('account/login').'");
						</script>';
			}
			
			$idaccount=0;

			if(isset($_SESSION['accountdeal'])){
							$mang=$_SESSION['accountdeal'];
					$idaccount=$mang[0];		
			}
			
			$uploadDir1 = 'upload/'.$idaccount.'/deal/'.$did_category.'/';
			$uploadDir = 'upload/'.$idaccount.'/deal/'.$did_category.'/';
			
			
			$filePath1=trim($_REQUEST['dsmall_image1']);
			$filePath2=trim($_REQUEST['dlarge_image1']);
			if (!file_exists($uploadDir)) mkdir($uploadDir, 0777, true);
			$deal=strtotime('now');
			
			if(isset($_FILES['dsmall_image'])){
				if($_FILES['dsmall_image']['name']!=""){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dsmall_image']['name']);
						$tmpName = $_FILES['dsmall_image']['tmp_name'];
						$fileSize = $_FILES['dsmall_image']['size'];
						$fileType = $_FILES['dsmall_image']['type'];
						$fileName="small_".$deal.'.jpg';
						$filePath1 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dsmall_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dsmall_image']['type'], $acceptable) && !empty($_FILES["dsmall_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath1)) {
				        echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
				$filePath1 = $uploadDir1 . $fileName;
				}
			}
			

			if(isset($_FILES['dlarge_image'])){
				if($_FILES['dlarge_image']['name']!=""){
				$error ="";
			    $maxsize    = 104857600;
			    $acceptable = array(
			        'image/jpeg',
			        'image/jpg',
			        'image/gif',
			    	'image/bmp',
			        'image/png'
			    );
						$fileName1=explode(".",$_FILES['dlarge_image']['name']);
						$tmpName = $_FILES['dlarge_image']['tmp_name'];
						$fileSize = $_FILES['dlarge_image']['size'];
						$fileType = $_FILES['dlarge_image']['type'];
						$fileName="large_".$deal.'.jpg';
						$filePath2 = $uploadDir . $fileName;
				$co=0;
				if(($_FILES['dlarge_image']['size'] >= $maxsize) ) {
			        $error = 'File too large. File must be less than 1 megabytes.'; $co++;
			    }
			
			    if(!in_array($_FILES['dlarge_image']['type'], $acceptable) && !empty($_FILES["dlarge_image"]["type"])) {
			        $error = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; $co++;
			    }
			
			    if($co == 0) {
					if(move_uploaded_file($tmpName, $filePath2)) {
				        echo "success";
					} else{
					       echo '<script>alert("Upload unsuccessfull !!!!");
			            			history.back();		</script>';
					       die();
					}
					
			    } else {
			            echo '<script>alert("'.$error.'");
			            			history.back();		</script>';
			        die(); //Ensure no more processing is done
				}
				$filePath2 = $uploadDir1 . $fileName;
				}
			}
			
			
			
			
			$dname=trim($_REQUEST['dname']);
			$dname = str_replace("'", "\'", ($dname));
			
			$dprice=trim($_REQUEST['dprice']);
			$dprice = str_replace( ',', '', $dprice);
			
			$dsale_off=trim($_REQUEST['dsale_off']);
			$dsale_off=str_replace( ',', '.', $dsale_off);
			
			$dprice_sale_off=trim($_REQUEST['dprice_sale_off']);
			$dprice_sale_off = str_replace( ',', '', $dprice_sale_off);
			
			$dtitle=str_replace("'", "", ($_REQUEST['dtitle']));
			
			$dadvantage=str_replace("'", "", ($_REQUEST['dadvantage']));
			$dcondition=str_replace("'", "", ($_REQUEST['dcondition']));
			$dcontent=str_replace("'", "", ($_REQUEST['dcontent']));
			
			$top_seller=0;
			if(isset($_REQUEST['top_seller']))
			$top_seller=1;
			
			$top_view=0;
			if(isset($_REQUEST['top_view']))
			$top_view=1;
			
			$top_100=0;
			if(isset($_REQUEST['top_100']))
			$top_100=1;
			
			$freeship=0;
			if(isset($_REQUEST['freeship']))
			$freeship=1;
			
			$delivery=trim($_REQUEST['delivery']);
			
			
			$this->load->model("mdeal");
			$this->mdeal->edit_deal($id_deal,$did_category,$dname,$dprice,$dsale_off,$dprice_sale_off,$filePath1,$filePath2,$dtitle,$dadvantage,$dcondition,$dcontent,$top_seller,$top_view,$top_100,$freeship,$delivery);
			
			echo '<script>
						alert("Update Deal successful");
						window.location.assign("'.base_url('account/list_deal_account').'");
						</script>';
			
		}
	}
?>